#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class CateCodeProvider : CY.GFive.Core.Providers.DALProvider.ICateCodeProvider
	{
		#region Sql
		private static readonly string SqlSelectCateCode = "SELECT [Id], [Code],[CateName],[CateDes],[ParentCode] FROM [CateCode] WHERE  [Id]=@Id";

		private static readonly string SqlInsertCateCode = "INSERT INTO [CateCode]([Code],[CateName],[CateDes],[ParentCode]) values(@Code,@CateName,@CateDes,@ParentCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateCateCode = "UPDATE [CateCode] SET [Code] = @Code,[CateName] = @CateName,[CateDes] = @CateDes,[ParentCode] = @ParentCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteCateCode = "DELETE [CateCode] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllCateCode = "SELECT [Id], [Code],[CateName],[CateDes],[ParentCode] FROM [CateCode]";
		#endregion

		#region ICateCodeProvider Members
		public Core.Business.CateCode Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectCateCode);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.CateCode cateCode = new Core.Business.CateCode();

				if (!reader.IsDBNull(0)) cateCode.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) cateCode.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) cateCode.CateName = reader.GetString(2);
				if (!reader.IsDBNull(3)) cateCode.CateDes = reader.GetString(3);
				if (!reader.IsDBNull(4)) cateCode.ParentCode = reader.GetString(4);

				return cateCode;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.CateCode cateCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, cateCode.Code);
			sql.AddParameter("@CateName", SqlDbType.NVarChar, cateCode.CateName);
			sql.AddParameter("@CateDes", SqlDbType.NVarChar, cateCode.CateDes);
			sql.AddParameter("@ParentCode", SqlDbType.NVarChar, cateCode.ParentCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertCateCode);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) cateCode.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.CateCode cateCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, cateCode.Code);
			sql.AddParameter("@CateName", SqlDbType.NVarChar, cateCode.CateName);
			sql.AddParameter("@CateDes", SqlDbType.NVarChar, cateCode.CateDes);
			sql.AddParameter("@ParentCode", SqlDbType.NVarChar, cateCode.ParentCode);
			sql.AddParameter("@Id", SqlDbType.Int, cateCode.Id);
			sql.ExecuteSql(SqlUpdateCateCode);
		}

		public void Delete(Core.Business.CateCode cateCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, cateCode.Id);
			sql.ExecuteSql(SqlDeleteCateCode);
		}

		public IList<Core.Business.CateCode> GetAllCateCode()
		{
			IList<Core.Business.CateCode> cateCodelist = new List<Core.Business.CateCode>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllCateCode);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.CateCode cateCode = new Core.Business.CateCode();

					if (!reader.IsDBNull(0)) cateCode.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) cateCode.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) cateCode.CateName = reader.GetString(2);
					if (!reader.IsDBNull(3)) cateCode.CateDes = reader.GetString(3);
					if (!reader.IsDBNull(4)) cateCode.ParentCode = reader.GetString(4);

					cateCode.MarkOld();
					cateCodelist.Add(cateCode);
				}
				reader.Close();
			}
			return cateCodelist;
		}
		#endregion
	}
}
